package ir.sourcecodes.animalsounds;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import androidx.core.view.ViewCompat;

public class XmlToClassAttribHandler {
    private final String KEY_TEXT = "text";
    private final String KEY_TEXT_COLOR = "textColor";
    private final String KEY_TEXT_SIZE = "textSize";
    private AttributeSet mAttributeSet;
    private Context mContext;
    private Resources mRes;
    private String namespace = "http://noghteh.ir";

    public XmlToClassAttribHandler(Context context, AttributeSet attributeSet) {
        this.mContext = context;
        this.mRes = context.getResources();
        this.mAttributeSet = attributeSet;
    }

    public String getTextValue() {
        String attributeValue = this.mAttributeSet.getAttributeValue(this.namespace, "text");
        if (attributeValue == null) {
            return "";
        }
        if (attributeValue.length() <= 1 || attributeValue.charAt(0) != '@' || !attributeValue.contains("@string/")) {
            return attributeValue;
        }
        Resources resources = this.mRes;
        return this.mRes.getString(resources.getIdentifier(this.mContext.getPackageName() + ":" + attributeValue.substring(1), (String) null, (String) null));
    }

    public int getColorValue() {
        String attributeValue = this.mAttributeSet.getAttributeValue(this.namespace, "textColor");
        if (attributeValue == null) {
            return ViewCompat.MEASURED_STATE_MASK;
        }
        if (attributeValue.length() <= 1 || attributeValue.charAt(0) != '@' || !attributeValue.contains("@color/")) {
            try {
                return Color.parseColor(attributeValue);
            } catch (Exception unused) {
                return ViewCompat.MEASURED_STATE_MASK;
            }
        } else {
            Resources resources = this.mRes;
            return this.mRes.getColor(resources.getIdentifier(this.mContext.getPackageName() + ":" + attributeValue.substring(1), (String) null, (String) null));
        }
    }

    public int getTextSize() {
        String attributeValue = this.mAttributeSet.getAttributeValue(this.namespace, "textSize");
        if (attributeValue == null) {
            return 12;
        }
        if (attributeValue.length() <= 1 || attributeValue.charAt(0) != '@' || !attributeValue.contains("@dimen/")) {
            try {
                return Integer.parseInt(attributeValue.substring(0, attributeValue.length() - 2));
            } catch (Exception unused) {
                return 12;
            }
        } else {
            Resources resources = this.mRes;
            return this.mRes.getDimensionPixelSize(resources.getIdentifier(this.mContext.getPackageName() + ":" + attributeValue.substring(1), (String) null, (String) null));
        }
    }

    public int gettextSizeUnit() {
        String attributeValue = this.mAttributeSet.getAttributeValue(this.namespace, "textSize");
        if (attributeValue == null) {
            return 2;
        }
        try {
            String substring = attributeValue.substring(attributeValue.length() - 2, attributeValue.length());
            if (substring.equals("dp")) {
                return 1;
            }
            if (substring.equals("sp")) {
                return 2;
            }
            if (substring.equals("pt")) {
                return 3;
            }
            if (substring.equals("mm")) {
                return 5;
            }
            if (substring.equals("in")) {
                return 4;
            }
            if (substring.equals("px")) {
                return 0;
            }
            return -1;
        } catch (Exception unused) {
        }
        return 0;
    }

}
